import Vue from 'vue';
import Router from 'vue-router';
import Login from './components/Login.vue';
import Home from './components/Home.vue';
import Welcome from './components/Welcome.vue'
import Users from './components/User/User.vue'
import Rights from './components/Rights/rights.vue'
import Roles from './components/Rights/roles.vue'
import Orders from './components/orders/orders.vue'
import Reports from './components/reports/reports.vue'
import Params from './components/goods/params.vue'
import Goods from './components/goods/goods.vue'
import Categories from './components/goods/categories.vue'
import Add from './components/goods/add.vue'



Vue.use(Router);

const router = new Router({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    { path: '/home', component: Home ,redirect:'/welcome', children:[
      {path:'/welcome',component:Welcome},
      {path:'/users',component:Users},
      {path:'/rights',component:Rights},
      {path:'/Roles',component:Roles},
      {path:'/orders',component:Orders},
      {path:'/reports',component:Reports},
      {path:'/params',component:Params},
      {path:'/goods',component:Goods},
      {path:'/categories',component:Categories},
      {path:'/goods/add',component:Add}

    ]}
  ]
});

//  配置路由守卫
router.beforeEach((to, from, next) => {
  if (to.path == '/login') return next();
  // 接下来判断是否有token 如果没有则返回到/login
  if (!sessionStorage.getItem('token')) {
    return next('/login');
  } else {
    next();
  }
});
export default router
